package com.pb.camera.jph.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.pb.camera.R;
import com.pb.camera.bean.DeviceMode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class SettingCameraNameActivity extends Activity implements OnClickListener {
    private ImageButton ib_back_add;
    private EditText et_add_name;
    private String mInputResult;
    private Button tv_name_success;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_camera_name);
        initView();
        initData();
    }

    private void initView() {
        ib_back_add = (ImageButton) findViewById(R.id.ib_back_add);
        et_add_name = (EditText) findViewById(R.id.et_add_name);
        tv_name_success = (Button) findViewById(R.id.btn_name_success);
    }

    private void initData() {
        ib_back_add.setOnClickListener(this);
        tv_name_success.setOnClickListener(this);

        //控制输入的字符个数和输入的符号
        final int maxLength = 5;
        et_add_name.addTextChangedListener(new TextWatcher() {
            private int cou = 0;
            int selectionEnd = 0;

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                cou = before + count;
                String editable = et_add_name.getText().toString();
                String str = stringFilter(editable); //过滤特殊字符
                if (!editable.equals(str)) {
                    et_add_name.setText(str);
                }
                et_add_name.setSelection(et_add_name.length());
                cou = et_add_name.length();
                if (cou > 0) {
                    tv_name_success.setBackgroundResource(R.drawable.add_button_bg);
                    tv_name_success.setEnabled(true);
                } else {
                    tv_name_success.setBackgroundResource(R.drawable.name_no);
                    tv_name_success.setEnabled(false);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (cou > maxLength) {
                    selectionEnd = et_add_name.getSelectionEnd();
                    s.delete(maxLength, selectionEnd);
                    tv_name_success.setBackgroundResource(R.drawable.add_button_bg);
                    tv_name_success.setEnabled(true);
                    //FIXME: 这里有问题！
                    mInputResult = s.toString();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_back_add:
                finish();
                break;
            case R.id.btn_name_success:
                DeviceMode device = (DeviceMode) getIntent().getSerializableExtra("device");
                if(device != null)
                    device.setDalias(et_add_name.getText().toString());
                Intent intent = new Intent(this, SettingCameraRoomActivity.class);
                intent.putExtra("device",device);
                this.startActivity(intent);
                break;
        }
    }

    private String stringFilter(String str) throws PatternSyntaxException {
        String regEx = "[/\\:*?<>|\"\n\t]"; //要过滤掉的字符
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
}
